// packageCommunity/pages/videoDetail/index.js
import{comments} from "../../../js/modules/mall.js"
import authorize from '../../../js/modules/authorize.js'
import BasePage from '../../../js/common/base-page' 
import {api,storage,util} from '../../../js/util'; 
import {router,pagesInfo} from '../../../js/router';
import {wholesaleSettings,share,config} from '../../../js/modules/gloab';

const app = getApp()

BasePage({
  validateLogin: false,
  /**
   * 页面的初始数据
   */
  data: {
    pageKey: "videoDetail",
    postDetail:{},//帖子详情
    imgDomain: api.imgDomain,
    videoDataList:[],//视频列表
    postId:'',//帖子id
    topBar:'',
    Currency:'',//货币符号
    NoLoginShowInteraction: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.data.topBar = res.statusBarHeight;
      },
      failure() {
        that.data.topBar = 0;
      }
    })
    this.setData({
      postId:options.id,
      topBar:that.data.topBar
    })
    this.getWholesaleConfig()
  },
  /**
  * 视频详情
  */
  getDataList:function(){
    var that = this;
    var data = {Id:this.data.postId};
    var list = [];
    comments.getPostDetail(data,function(content){
      console.log(content)
      if(content.Success){
        let storeInfo = authorize.getEntity() || {};
        let manager = storage.manager || {}
        // console.log(content,'---con');
        var data = content.Content || '';
        if(data.FKFlag == storeInfo.fkFlag && data.ManagerId == manager.ManagerId){
          data.userDel = true;
        }else{
          data.userDel = false;
        }
        data.Content = data.Content.replace(/<[^>]+>/g, "").replace(/()+/ig, "\n")
        list.push({
          Id:data.Id,
          Video:data.Video,
          HeaderSrc:data.HeaderSrc,
          Publisher:data.Publisher,
          LikeCount:data.LikeCount,
          CommentCount:data.CommentCount,
          Content:data.Content,
          IsSupport:data.IsSupport,
          Products:data.Products,
          FKFlag:data.FKFlag,
          FKId:data.FKId,
          ProprietorId:data.ProprietorId,
          Proprietor:data.Proprietor,
          ManagerId:data.ManagerId,
          Face:data.Face
        })
        that.setData({
          videoDataList:list,
          postDetail:data
        })
      }else{
        util.tips(content.Message);
      }
    }) 
  },
    /**
   * 获取配置信息
   */
  getWholesaleConfig() {
    var that = this;
    var fkid = app.dns.proprietorId;
    var fkflag = app.dns.proprietor;
    config.getWholesaleConfigs(
      fkid,
      fkflag,
      "Wholesale",
      function (model) {
        if (model.NoLoginShowInteraction == false) {
          if (!authorize.isLogin('store')) {
            authorize.redirect('store')
            return
          }
        }
        that.setData({
          NoLoginShowInteraction: model.NoLoginShowInteraction
        })
        that.getDataList()
      });
  },
  /**
   * 返回
  */
  bindBack:function(){
    let pages = getCurrentPages();
    if (pages.length > 1) {
      wx.navigateBack();
    }else{
      wx.reLaunch({
        url: '/pages/index',
      })
    }
  },
  /**
   * 删除
  */
  bindDel:function(e){

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.setData({
    //   Currency:api.Currency || '￥'
    // })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    let {imgDomain,postDetail} = that.data;
    let path = pagesInfo.videoDetail.url + `?id=${postDetail.Id}`;
    // todo: 图片调为5:4
    return {
      title: postDetail.MainText?postDetail.MainText:postDetail.Content,
      imageUrl: imgDomain+postDetail.Image,
      path: path
    }
  }
})